import { IUser, ICompany, IRecruit, IKey, IFindRecruit } from '../types'
import axios from 'axios'

interface BaseRes {
  stat: string
  message?: string
}

interface UserInfo extends BaseRes {
  data: IUser
}

interface UserList extends BaseRes {
  rows: IUser[]
}

interface CompanyList extends BaseRes {
  rows: ICompany[]
}

interface CompanyInfo extends BaseRes {
  result: ICompany
}

interface RecruitList extends BaseRes {
  rows: IRecruit[]
}

interface RecruitInfo extends BaseRes {
  result: IRecruit
}

// 首页4个api
// 热门职位：find()查key表
export async function hotposition() {
  const result = await axios.post<IKey>('/api/home/keyword')
  return result.data
}

// 热门企业：find()查company表
export async function hotcompany() {
  const result = await axios.post<ICompany>('/api/home/company')
  return result.data
}

// 推荐职位：keys：根据搜索栏的筛选查找Recruit表，不传则返回整个表
export async function hotrecruit(props: IFindRecruit) {
  const result = await axios.post<IRecruit>('/api/home/recruit', { props })
  return result.data
}

// 获取用户信息：和个人信息页获取用户信息共用api
export async function userInfo() {
  const result = await axios.post<UserInfo>('/api/user/front/getInfo')
  return result.data
}

// 职位详细信息页面 3个api
// 招聘详细信息：id：Recruit表存储的id
export async function recruit(id: string) {
  const result = await axios.post<RecruitInfo>('/api/recruit/detail', { id })
  return result.data
}

// 获取公司详细信息：id：招聘信息表中存储的公司id
export async function company(id: string) {
  const result = await axios.post<CompanyInfo>('/api/company/detail', { id })
  return result.data
}

// 相似职位：keys：招聘详细信息中的关键词； area：招聘信息表中的地点；salary：薪资； 查招聘信息表
export async function similarRecruit(keys: string, area: string, salary: string) {
  const result = await axios.post<IRecruit>('/api/recruit/detail/similar', { keys, area, salary })
  return result.data
}

// 公司详情页 2个api 包含公司详细信息
// 获取公司热招职位：id：公司表中的id查招聘信息表
export async function companyRecruit(id: string) {
  const result = await axios.post<IRecruit>('/api/company/allrecruit', { id })
  return result.data
}

// 登陆页面 2个api
// 用户登录
export async function userlogin(username: string, password: string) {
  const result = await axios.post<IUser>('/user/login', { username, password })
  return result.data
}

// 注册
export async function register(record: IUser) {
  const result = await axios.post<IUser>('/api/user/register', { record })
  return result.data
}

// 个人信息页面 2个api
// 用户注销
export async function logout() {
  return await axios.post('/api/user/logout')
}

// 管理端登陆页面
// 管理端登录
export async function login(username: string | undefined, password: string | undefined) {
  const result = await axios.post('/api/user/admin/login', { username, password })
  return result.data
}

// 我的企业页面 4个api
// 获取公司列表 id:管理者id
export async function companylist() {
  const result = await axios.post<CompanyList>('/api/company/admin/companylist')
  return result.data
}

// 添加公司,改为any类型就不用改变更多的东西传参时
export async function addcompany(record: ICompany | undefined) {
  const result = await axios.post<BaseRes>('/api/company/admin/add', record)
  return result.data
}

// 编辑公司
export async function updateCompany(record: any) {
  const result = await axios.post<BaseRes>('/api/company/admin/update', record)
  return result.data
}

// 删除公司
export async function removeCompany(id: string | undefined) {
  const result = await axios.post<BaseRes>('/api/company/admin/remove', { id })
  return result.data
}


// 企业成员页面 3个api
// 获取企业用户列表  id：公司id
export async function companyUser(id: string) {
  const result = await axios.post<UserList>('/api/user/company/userlist', { id })
  return result.data
}

// 添加企业用户
export async function addCompanyUser(username: string, companyId: string) {
  const result = await axios.post<BaseRes>('/api/user/company/userAdd', { username, companyId })
  return result.data
}

// 删除企业用户
export async function removeCompanyUser(id: string) {
  const result = await axios.post<BaseRes>('/api/user/user/remove', { id })
  return result.data
}

// 我的职位 4个api
// 获取发布职位的列表 id：hr的id
export async function recruitlist(id: string) {
  const result = await axios.post<RecruitList>('/api/recruit/admin/recruitList', { id })
  return result.data
}

// 添加职位
export async function addRecruit(record: any) {
  const result = await axios.post<BaseRes>('/api/recruit/admin/add', record)
  return result.data
}

// 编辑职位
export async function updateRecruit(record: any) {
  const result = await axios.post('/api/recruit/admin/edit', record)
  return result.data
}

// 删除职位
export async function removeRecruit(id: string | undefined) {
  const result = await axios.post('/api/recruit/admin/remove', { id })
  return result.data
}

// 系统管理员
export async function allUser() {
  const result = await axios.post<UserList>('/api/user/userlist')
  return result.data
}

export async function statusUser(id: string) {
  const result = await axios.post<BaseRes>('/api/user/status', { id })
  return result.data
}

export async function statusCompany(id: string | undefined) {
  const result = await axios.post<BaseRes>('/api/company/status', { id })
  return result.data
}
